#!/bin/bash

# 配置 Harbor 仓库信息
HARBOR_IP="172.29.82.95"
HARBOR_PORT="80"
HARBOR_USER="admin"
HARBOR_PASSWORD="123456"
PROJECT_NAME="test"  # 项目名
IMAGE_NAME="mysql"  # 镜像名称
IMAGE_TAG="8.0.19"  # 镜像标签

# 完整的镜像路径（包含项目名）
FULL_IMAGE_NAME="${HARBOR_IP}:${HARBOR_PORT}/${PROJECT_NAME}/${IMAGE_NAME}:${IMAGE_TAG}"

# 登录 Harbor 仓库
echo "Logging into Harbor..."
docker login -u $HARBOR_USER -p $HARBOR_PASSWORD $HARBOR_IP:$HARBOR_PORT
if [ $? -ne 0 ]; then
    echo "Failed to login to Harbor. Exiting..."
    exit 1
fi

# 构建镜像
echo "Building Docker image..."
docker build -t $IMAGE_NAME:$IMAGE_TAG .
if [ $? -ne 0 ]; then
    echo "Failed to build Docker image. Exiting..."
    exit 1
fi

# 标记镜像（添加 Harbor 仓库地址和项目名）
echo "Tagging Docker image..."
docker tag $IMAGE_NAME:$IMAGE_TAG $FULL_IMAGE_NAME
if [ $? -ne 0 ]; then
    echo "Failed to tag Docker image. Exiting..."
    exit 1
fi

# 推送镜像到 Harbor
echo "Pushing Docker image to Harbor..."
docker push $FULL_IMAGE_NAME
